package avicit.bdp.dcs.json.service;

import avicit.bdp.common.datasource.*;
import avicit.bdp.common.utils.enums.JobType;
import avicit.bdp.dcs.datasource.service.DsDatasourceService;
import avicit.bdp.dcs.json.service.builder.ApiJsonBuilder;
import avicit.bdp.dcs.json.service.builder.FileJsonBuilder;
import avicit.bdp.dcs.json.service.builder.StreamJsonBuilder;
import avicit.bdp.dcs.json.service.builder.StructureJsonBuilder;
import avicit.bdp.dcs.plugin.ParamPropertyUtil;
import avicit.bdp.dcs.task.dto.TaskDTO;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liyb json构建实现类
 */
@Service
@Transactional
public class DataxJsonService {

  @Autowired private DsDatasourceService dsDatasourceService;
  @Autowired private StructureJsonBuilder structureJsonBuilder;
  @Autowired private ApiJsonBuilder apiJsonBuilder;
  @Autowired private FileJsonBuilder fileJsonBuilder;
  @Autowired private StreamJsonBuilder streamJsonBuilder;

  /**
   * 构建DataxJson
   *
   * @param task
   * @return
   * @throws Exception
   */
  public String buildDataxJson(TaskDTO task) throws Exception {
    if (task.getJobType() == JobType.TRANSFER.getCode()
        || task.getJobType() == JobType.SYNC.getCode()) {
      // 应用数据采集
      return structureJsonBuilder.buildDataxJsonByStructure(task.getId());
    } else if (task.getJobType() == JobType.SEMISTRUCTURE.getCode()) {
      // 文件采集
      return fileJsonBuilder.buildDataxJsonByFile(task.getId());
    } else if (task.getJobType() == JobType.STREAM.getCode()) {
      // 流式采集
      return streamJsonBuilder.buildDataxJsonByStream(task.getId());
    } else if (task.getJobType() == JobType.API_HTTP.getCode()) {
      // API采集
      return apiJsonBuilder.buildDataxJsonByAPI(task.getId());
    } else {
      return null;
    }
  }

  public Map<String, String> getDataxPluginParam(String datasourceId) throws Exception {
    BaseDataSource baseDataSource = dsDatasourceService.getDatasource(datasourceId);
    return ParamPropertyUtil.getDataxPluginParam(baseDataSource.type());
  }
}
